var emojMsg = '写下你的评论说不定会掀起一片风浪😎'
$(document).ready(function () {

    // 绑定回车键事件
    $('#search-btn').on('keypress', function (e) {
        if (e.which === 13) { // 回车键
            triggerSearch();
        }
    });

    // 绑定点击搜索图标事件
    $('#search-icon').on('click', function () {
        triggerSearch();
    });

    // 搜索触发函数
    function triggerSearch() {
        const keyword = $('#search-btn').val().trim(); // 获取并去掉前后空格
        if (localStorage.getItem("lastReqKeyword") === '' && keyword === '') {
            return;
        }
        localStorage.setItem("lastReqKeyword", keyword);

        // 获取分页参数
        const current = $('#current-btn').val();
        const size = $('#size-btn').val();

        // TODO 等以后链接优化
        window.location.href = `/index.html?keyword=${keyword}&current=${current}&size=${size}`
    }

    $(".navbar-burger").click(function () {
        $(".navbar-burger").toggleClass("is-active");
        $(".navbar-menu").toggleClass("is-active");
    });
    $("#nav-link").click(function () {
        $("#nav-dropdown-item").toggleClass("nav-dropdown-item");
    })


    $("#comment-submit-btn").click(function () {
        addComment($('#comment-box'), (data, parentCommentId) => {
            addChildrenComment(data, parentCommentId)
        });
    })

    $("#comment-chat-modal-submit").click(function () {
        addComment($('#modal-comment-box'), (data, parentCommentId) => {
            addChildrenComment(data, parentCommentId);
            addModalChildrenComment(data);
        });
    })


    // 关闭模态框（通过点击关闭按钮）
    $('#comment-chat-modal-close').click(function () {
        $('#comment-chat-modal').removeClass('is-active');
    });


    // 到顶部的函数
    $('#scrollToTopBtn').on('click', function () {
        $('html, body').animate({scrollTop: 0}, 'smooth');
    });

    // 到底部的函数
    $('#scrollToBottomBtn').on('click', function () {
        $('html, body').animate({scrollTop: $(document).height()}, 'smooth');
    });

    $("#share-article-btn").on('click', function () {
        const title=document.title;
        const pageAuthor = $("#create-user-nickname").text();
        const pageURL = window.location.href;
        shareArticleOrComment(title,pageAuthor,pageURL);
    })

    $("#share-close-btn").on('click', function () {
        $("#shareModal").removeClass("is-active");

    })

});

function articleLikeCount(articleId) {
    $.ajax({
        url: `/question/like-count/${articleId}`,
        contentType: "application/json",
        type: 'PUT',
        success: function (res) {
            if (res.code === 200) {
                $("#article-like-count").text(res.data);
                $("#article-like-count-btn").toggleClass("is-outlined")
            } else if (res.code === 401) {
                // TODO 等以后优化跳转,把跳转的请求存储到栈内，等登录完成后在跳转
                // window.location.href = "/login.html"
                showErrorToast(res.msg);
            } else {
                console.log("错误:" + res)
            }
        },
        error: function (err) {
            console.log("错误:" + err)
        }
    });
}

function edit(id) {
    window.location.href = `/article/${id}`;
}

// 删除
function del(id) {
    $.ajax({
        url: `/article/${id}`,
        type: 'DELETE',
        success: function (res) {
            if (res.code === 200) {
                showSuccessToast()
                window.location.href = "/article/list";
            } else {
                showErrorToast(res.msg);
            }
        },
        error: function (err) {
            // 确保错误处理与服务器响应结构相匹配
            showErrorToast(err);
        }
    });
}
